/*
 * Created by SharpDevelop.
 * User: Guy Fomi
 * Date: 2/6/2011
 * Time: 2:14 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.Web.Script.Serialization;

namespace TomatoSynchronizer.Store
{
	/// <summary>
	/// Description of ProductStore.
	/// </summary>
	public class ProductStore
	{
		private Site _site;
		private HtmlWeb _client;
		
		public ProductStore(Site site,HtmlWeb client)
		{
			_site = site;
			_client = client;
		}
		
		private IDictionary GetProduct(string ProductId)
		{
			List<IDictionary> entries = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=load_product&module=products&products_id=" + ProductId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "data")
				{
					IDictionary Product = (IDictionary)json[key];
					entries.Add(Product);
				}
			}
			
			return entries[0];
		}
		
		public List<IDictionary> GetProducts()
		{
			List<IDictionary> records = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=list_products&categories_id=0&module=products");
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary record = (IDictionary)recs[i];
						IDictionary Product = GetProduct(record["products_id"].ToString());
						Product["attributes"] = GetProductAttributes(record["products_id"].ToString());
						Product["images"] = GetProductImages(record["products_id"].ToString());
						Product["variants_products"] = GetProductVariants(record["products_id"].ToString());
						Product["xsell_products"] = GetProductXsell(record["products_id"].ToString());
						Product["products_attachments"] = GetProductAttachments(record["products_id"].ToString());
						Product["accessories"] = GetProductAccessories(record["products_id"].ToString());
						records.Add(Product);
					}
				}
			}
			
			return records;
		}
		
		public List<IDictionary> GetProductXsell(string productId)
		{
			List<IDictionary> xsell_products = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=get_xsell_products&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary xsell_product = (IDictionary)recs[i];
						xsell_products.Add(xsell_product);
					}
				}
			}
			
			return xsell_products;
		}

		public List<IDictionary> GetProductAccessories(string productId)
		{
			List<IDictionary> accessories = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=get_accessories&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary accessory = (IDictionary)recs[i];
						accessories.Add(accessory);
					}
				}
			}
			
			return accessories;
		}
		
		public List<IDictionary> GetProductAttachments(string productId)
		{
			List<IDictionary> products_attachments = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=load_products_attachments&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary product_attachment = (IDictionary)recs[i];
						products_attachments.Add(product_attachment);
					}
				}
			}
			
			return products_attachments;
		}
		
		public List<IDictionary> GetProductVariants(string productId)
		{
			List<IDictionary> variants_product = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=get_variants_products&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary variant_product = (IDictionary)recs[i];
						variants_product.Add(variant_product);
					}
				}
			}
			
			return variants_product;
		}
		
		public List<IDictionary> GetProductImages(string productId)
		{
			List<IDictionary> images = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=get_images&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "records")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary image = (IDictionary)recs[i];
						if(image["name"] != null)
						{
							if(!string.IsNullOrEmpty(image["name"].ToString()))
							{
								Utility.DownloadImage(_site.Url + "/images/products/originals/" + image["name"],"/images/products/originals/");
							}
						}
						
						images.Add(image);
					}
				}
			}
			
			return images;
		}
		
		public List<IDictionary> GetProductAttributes(string productId)
		{
			List<IDictionary> attributs = new List<IDictionary>();
			
			HtmlDocument doc = _client.Load(_site.Url + "/admin/json.php?action=get_attributes&module=products&products_id=" + productId);
			HtmlNode node = doc.DocumentNode.SelectSingleNode("/");
			
			JavaScriptSerializer serialiser = new JavaScriptSerializer();
			IDictionary json = serialiser.Deserialize<IDictionary>(node.InnerText);
			foreach(string key in json.Keys)
			{
				if(key == "attributes")
				{
					object[] recs = (object[])json[key];
					for(int i = 0;i < recs.Length;i++)
					{
						IDictionary attribut = (IDictionary)recs[i];
						attributs.Add(attribut);
					}
				}
			}
			
			return attributs;
		}
	}
}
